package com.shop.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.admin.dao.RuleNeedDao;
import com.shop.admin.dto.CouponRuleDTO;
import com.shop.admin.entity.RuleNeed;
import com.shop.admin.service.RuleNeedService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RuleNeedServiceImpl extends ServiceImpl<RuleNeedDao, RuleNeed> implements RuleNeedService {
    @Override
    public void saveRule(CouponRuleDTO ruleNeed, Integer ruleId) {
        List<String> couponTypeNeed = ruleNeed.getCouponTypeNeed();
        couponTypeNeed.stream().map(item->{
            RuleNeed need = new RuleNeed();
            RuleNeed ruleNeedBuild = need.toBuilder().ruleId(ruleId).couponTypeNeed(item).couponRangeNeed(ruleNeed.getCouponRangeNeed()).build();
            return this.baseMapper.insert(ruleNeedBuild);
        }).collect(Collectors.toList());
    }

    @Override
    public void updateRule(CouponRuleDTO couponRuleDTO, Integer ruleId) {
        this.baseMapper.delete(new QueryWrapper<RuleNeed>().lambda().eq(RuleNeed::getRuleId,ruleId));
        couponRuleDTO.getCouponTypeNeed().stream().map(item ->{
            RuleNeed need = new RuleNeed();
            RuleNeed ruleNeedBuild = need.toBuilder().ruleId(ruleId).couponTypeNeed(item).couponRangeNeed(couponRuleDTO.getCouponRangeNeed()).build();
            return this.baseMapper.insert(ruleNeedBuild);
        }).collect(Collectors.toList());
    }

}
